package cn.chint.manage.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.chint.manage.dao.AdminDao;
import cn.chint.manage.service.AdminService;
import cn.chint.po.SysUser;
import cn.chint.po.User;

import com.alibaba.fastjson.JSONObject;

@Controller
public class UserLoginController { 


	@Autowired
	AdminService adminService;
	@Autowired
	AdminDao adminDao ;
	
	public Logger log = Logger.getLogger(getClass());
	
	
	
	
	
	public static  List<String>  ls = new ArrayList<String>();
	@RequestMapping("/list")
	public String list(HttpServletRequest request,HttpServletResponse response) throws IOException, InterruptedException{
		
		
		for(int i=0;i<100000;i++){
			Thread.sleep(2000);
			ls.add("test"+i);
			if(i==50){
				break;
			}
		}
		
		return null;
	}
	@RequestMapping("/test")
	public String test(HttpServletRequest request,HttpServletResponse response) throws IOException, InterruptedException{
		
		for(int i=0;i<ls.size();i++){
			Thread.sleep(10);
			System.out.println(ls.get(i));
//			ls.remove(i);
		}
		
//		for(int i=0;i<ls.size();i++){
//			Thread.sleep(10);
//			System.out.println("List 剩下元素："+ls.get(i));
//		}
		return null;
	}
	@RequestMapping("/del")
	public String del(HttpServletRequest request,HttpServletResponse response) throws IOException, InterruptedException{
		
		for(int i=0;i<ls.size();i++){
			ls.remove(i);
		}
		return null;
	}
	
	
    @RequestMapping(value = "userLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String  userLogin(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
    	//InputStream in = request.getInputStream();
    	//String reqJson = org.apache.commons.io.IOUtils.toString(in, "utf-8"); 
    	//JSONObject jb = JSONObject.fromObject(reqJson);
    	JSONObject jsonObj = new JSONObject();
    	try{
	    	String s_form_data = request.getParameter("s_form_data");
	    	com.alibaba.fastjson.JSONObject jb  = JSONObject.parseObject(s_form_data);
	    	
	    	String username=jb.getString("username");
	    	String password=jb.getString("password");
	    	log.info(username);
	    	log.info(password);
	    	
	
	    	SysUser sysUser = adminService.findChannelUser(username,password);
	    	
	    	if(sysUser==null){
	            jsonObj.put("status", false);
	    		jsonObj.put("msg", "登录失败，账户或密码错误。");
	    	}else{
	    		    if(sysUser.getStatus().intValue()==0){
	    		    	jsonObj.put("status", false);
	    	    		jsonObj.put("msg", "您的账户已被冻结,请联系管理员");
	    		    }else{
	    		    	request.getSession().setAttribute("sysUser", sysUser);
	        		    jsonObj.put("status", true);
	    		    }
	    	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return jsonObj.toString();
    	 
    }


    /**
	 * 跳转到首页
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/direct_home")
	public String direct_home(HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		log.info("跳转首页");
		response.sendRedirect(request.getContextPath() + "/lay/layui/index.html");
		
		return null;
	}

	
    /**
	 * 首页获取用户和菜单
	 * @return
	 * @throws IOException 
	 */
    @RequestMapping(value = "get_channel_user",  produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String  get_channel_user(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
    	
    	SysUser user = (SysUser)request.getSession().getAttribute("sysUser");
    	JSONObject jsonObj = new JSONObject();
    	int level=user.getLevel();
    	String  menu;
    	if (level==0) {
    		menu =
	    	    	"<li class=\"layui-nav-item layui-this\">"+
	    	         "  <a href=\"javascript:;\" data-url=\"msg_enter.html\" data-id=\"1\">项目列表</a>"+
	    	        "</li>" +
	    	        "<li class=\"layui-nav-item layui-this\">"+
   	    	         "  <a href=\"javascript:;\" data-url=\"to_change.html\" data-id=\"2\">项目接收人</a>"+
   	    	        "</li>" +
	    	        "<li class=\"layui-nav-item layui-this\">"+
  	    	         "  <a href=\"javascript:;\" data-url=\"focus_user.html\" data-id=\"3\">关注用户列表</a>"+
  	    	        "</li>"+
					"<li  class=\"layui-nav-item layui-this\">" +
							"<a href=\"javascript:;\" data-url=\"Review.html\" data-id=\"4\">新增接受人</a>"+
					"</li>";
		} else {
			menu =
	    	    	"<li class=\"layui-nav-item layui-this\">"+
	    	         "  <a href=\"javascript:;\" data-url=\"admin.html\" data-id=\"1\">项目列表</a>"+
	    	        "</li>" +
	    	        "<li class=\"layui-nav-item layui-this\">"+
   	    	         "  <a href=\"javascript:;\" data-url=\"to_change.html\" data-id=\"2\">项目接收人</a>"+
   	    	        "</li>" +
	    	        "<li class=\"layui-nav-item layui-this\">"+
 	    	         "  <a href=\"javascript:;\" data-url=\"focus_user.html\" data-id=\"3\">关注用户列表</a>"+
 	    	        "</li>" +
	    	        "<li class=\"layui-nav-item layui-this\">"+
	    	         "  <a href=\"javascript:;\" data-url=\"orderoperate.html\" data-id=\"4\">订单列表</a>"+
	    	        "</li>"+
							"<li  class=\"layui-nav-item layui-this\">" +
							"<a href=\"javascript:;\" data-url=\"Review.html\" data-id=\"5\">新增接受人</a>"+
							"</li>";
		}
    	

		jsonObj.put("menu", menu);
		jsonObj.put("channel_user_name", user.getUsername());
		jsonObj.put("channel_user_id", user.getId());

    	
    
    	return jsonObj.toString();
    	 
    }
	
    @RequestMapping(value = "exit_login",  produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String  exit_login(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
    	
    	request.getSession().invalidate();
    	
    	response.sendRedirect(request.getContextPath() + "/login.html");

    	
    	return null;
    	 
    }
	@SuppressWarnings("unused")
	@RequestMapping(value = "amend",  produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String amend(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response){
		JSONObject jsonObj = new JSONObject();

		String  password = request.getParameter("password");
		SysUser user = (SysUser) request.getSession().getAttribute("sysUser");
		String username = user.getUsername();
		if(password != null || password != ""){
			adminDao.amend(password,username);
			jsonObj.put("status","50");
			jsonObj.put("msg","修改成功");
		}else{
			jsonObj.put("status","0");
			jsonObj.put("msg","修改失败");
		}
		return jsonObj.toString();
	}


}





